#ifndef INTERPOLATOR_H
#define INTERPOLATOR_H

#include <animconst.h>
#include <functional>

template <typename T> class Interpolator
{
public:
    Interpolator(T& target, T begin, T end, double now, double period, AccelType accel) :
        mFrom(begin), mTotalDelta(end - begin), mTarget(target), mPeriod(period), mBegin(now),
        mEnd(period + now)
    {
        mWorking = true;
        switch(accel)
        {
        case Accelerate: // UNIMPLEMENTED
        case Deaccelerate: // UNIMPLEMENTED
        case Linear:
            mfInterpolate = LinearInterpolation;
        }

        mfInterpolate(this, now);
    }

    void Interpolate(double now)
    {
        mTarget = mfInterpolate(this, now);
    }

    bool IsWorking()
    {
        return mWorking;
    }

    double EndTime()
    {
        return mEnd;
    }

private:
    T mFrom, mTotalDelta, &mTarget;
    double mPeriod, mBegin, mEnd;
    std::function<T(Interpolator*, double)> mfInterpolate;
    bool mWorking;

private:
    static T LinearInterpolation(Interpolator* zis, double now)
    {
        if(!zis->mWorking) return T();
        else if(zis->mEnd < now)
        {
            zis->mWorking = false;
            now = zis->mEnd;
        };

        return ((now - zis->mBegin) / zis->mPeriod) * zis->mTotalDelta + zis->mFrom;
    }
};

#endif // INTERPOLATOR_H
